<?php
/**
 * Medical Monitoring PDF Generation
 * This module will generate the PDF containing all monitoring information
 * 
 * @author Xavier VILAIN
 * @package Nawa\MedicalStudy\MedicalCenter\Monitoring
 * @license #license-nawa#
 * 
 */

use Nawa\MedicalStudy\MedicalCenter\Patient\Status;
use Fox\Generator;
use Fox\Html\TextArea;
use Nawa\Messages;
use Nawa\Customer;
use Fox\Html\DropList;
use Fox\Html\Block;
use Fox\Html\Input;
use Nawa\MedicalStudy\Authorization;
use PHPPDF\Fpdf;
Fox::instance()->usePDF();
$xview = Xview::instance();

$msid = $xview->val('msid');
$mcid = $xview->val('mcid');
$monitoringid = $xview->val('monitoringid');
$keys = array('msid' => $msid,'mcid' => $mcid,'monitoringid' => $monitoringid);
$ms = $xview->component('medicalstudies','object',$keys);
$msData = $ms->getData();
if(Authorization::check(Authorization::MS_UPDATE,$ms)) {
	$update = true;
}
elseif(!Authorization::check(Authorization::MS_VIEW,$ms)) {
	$xview->accessDenied();
}
// Medical Center
$centerPartner = $ms->findMedicalCentersPartners($msid.':'.$mcid);
$medicalCenter = $centerPartner->medicalCenter();
$mcData = array_merge($centerPartner->getData(),$medicalCenter->getData());
$mcData['reference_extern'] = $mcData['reference'];
$mcData['mc_name'] = $mcData['name1'];
unset($mcData['reference']);
$mcData['mc_address'] = Fox\Utilities\Format::address($medicalCenter->address());

// Monitoring
$monitoring = $centerPartner->findMonitorings($centerPartner->getInLineId().':'.$monitoringid);
$monitoringData = $monitoring->getData();
$monitoringData['mc_report_date'] = $monitoring->udate;
try {
	$category = new Nawa\Survey\Category($monitoringData['category']);
	$title = $category->label();
} catch(Fox\Object\Exception\NotExist $e) {
	$title = null;
}

$pdf = new Nawa\MedicalStudy\MedicalCenter\Monitoring\Pdf();
$cellHeight = 5;
$pdf->title($title);
$reference = $monitoring->monitoringid;
if(empty($reference)) {
	$reference = ' ';
}
$pdf->setReference(Msg(Messages::BUNDLE,Messages::REPORT_REF_X,$reference));
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->footerMessage(Msg(Messages::BUNDLE,Messages::DATE_X_SIGNATURE));

// 1. Reference to the study block
$blStudyRef = new Block('bStudyRef',Msg(Messages::BUNDLE,Messages::STUDY_REFERENCE));
$blStudyRef->add(new DropList(array("ms_customer","customerid"),true,false,true))
			->options(Customer::customers(),"customerid","name1");
// Reference
$blStudyRef->add(new Input(array("ms_reference","reference"),true));
// Short Description
$blStudyRef->add(new Input(array("ms_label","label")))->size(Input::SIZE_XLARGE);
$blStudyRef->initialize($msData);
// 2nd Column
$blStudyRef->add(new Input(array('mc_reference','reference_extern'),false),2,'customerid')
			->label(Msg(Messages::BUNDLE,Messages::CENTER_REF));
$blStudyRef->add(new Input(array('name1','mc_name'),false),2,'reference')
			;
$blStudyRef->add(new TextArea(array('address','mc_address'),false),2,'label')
			;
$blStudyRef->initialize($mcData);
$blStudyRef->renderAsPdf()->render($pdf);

// 2. Monitoring Reference
$yStartPosition = $pdf->GetY();
$blMonitoringRef = new Block('bMonitoringRef',Msg(Messages::BUNDLE,Messages::MONITORING_REFERENCE));
$blMonitoringRef->add(new Input('monitoring_date'));
// Type Of Monitoring Interview
$interviewTypes = Fox\Dao::finder(Nawa\MedicalStudy\MedicalCenter\Monitoring\Interview\Type::className())
					->find();
$blMonitoringRef->add(new DropList(array('mo_interview_type','interview_type'),false,true,2))
	->options($interviewTypes,'interview_type',Generator::call('label'));
$blMonitoringRef->add(new Input('mc_report_date'));
$blMonitoringRef->initialize($monitoringData);
$blMonitoringRef->renderAsPdf()->render($pdf);
$xEndPosition = $pdf->GetX();
$yEndPosition = $pdf->GetY();
// Validation Box
$pdf->SetY($yStartPosition+7);
$pdf->SetX(120);
$pdf->SetTextColor(255,0,0); // Red
$pdf->SetDrawColor(255,0,0); // Red

$validation  = strtoupper(Msg(Messages::BUNDLE,Messages::VALIDATION));
$pdf->underline = true;
$pdf->Cell(70,5,$validation,0,0,'C');
$pdf->underline = false;
$pdf->Ln();
$pdf->SetX(120);
$validation  = Msg(Messages::BUNDLE,Messages::DATE).":\n";
$validation .= Msg(Messages::BUNDLE,Messages::NAME).":\n";
$validation .= Msg(Messages::BUNDLE,Messages::SIGNATURE).":\n";
$pdf->Rect(120,$yStartPosition+7,70, 22);
$pdf->MultiCell(70,5,$validation);
$pdf->SetTextColor(0,0,0); // Red
$pdf->SetDrawColor(0,0,0); // Red
// Reset Positon
$pdf->SetY($yEndPosition+5);
$pdf->SetX($xEndPosition);
$pdf->Ln();

// 3. Partners
$blPartners = new Block('bPartnersList',Msg(Messages::BUNDLE,Messages::PARTNERS));
$blPartners->renderAsPdf()->render($pdf);
foreach($monitoring->partnersData() as $partnerData) {
	// Name 
	$pdf->Cell(5);
	$pdf->SetFont('','',$pdf::FONT_SIZE_NORMAL);
	$pdf->Cell(50,$cellHeight,$partnerData['name1']);
	//$pdf->Cell(50,$cellHeight,$partnerData['name2']);
	$pdf->Ln();
}
$pdf->Ln();
// 3. Patient List
$patients = $monitoring->patients();
if(count($patients) > 0) {
	
	$blPatients = new Block('bCheckList',Msg(Messages::BUNDLE,Messages::PATIENT_LIST));
	$blPatients->renderAsPdf()->render($pdf);
	// Display Header
	$pdf->SetFont('','B',$pdf::FONT_SIZE_NORMAL);
	$pdf->Cell(5);
	$pdf->Cell(50,$cellHeight,\Fox\Ddic\DataElement\Text::label('patient_ref'),true);
	$pdf->Cell(40,$cellHeight,\Fox\Ddic\DataElement\Text::label('birth_date'),true,0,'C');
	$pdf->Cell(40,$cellHeight,\Fox\Ddic\DataElement\Text::label('selection_date'),true,0,'C');
	$pdf->Cell(40,$cellHeight,\Fox\Ddic\DataElement\Text::label('status'),true,0,'C');
	
	$pdf->Ln();
	// Display Patient Grid Body
	$pdf->SetFont('','',$pdf::FONT_SIZE_NORMAL);
	$statusLabel = array();
	$statuses = Fox\Dao::finder(Status::className())
		->where('disable = ?',array(0))
		->find();
	foreach($statuses as $status) {
		$statusLabel[$status->status] = $status->label;
	}
	foreach($patients as $patient) {
		$pdf->Cell(5);
		$pdf->Cell(50,$cellHeight,$patient->reference,true);
		$pdf->Cell(40,$cellHeight,Fox\Utilities\DateTimes::toFullString($patient->birth_date,false,false),true,0,'C');
		$pdf->Cell(40,$cellHeight,Fox\Utilities\DateTimes::toFullString($patient->selection_date,false,false),true,0,'C');
			$pdf->Cell(40,$cellHeight,$statusLabel[$patient->status],true,0,'C');
		$pdf->Ln();
	}
	$pdf->Ln();
}
// 4. Points Discussed
$blCk = new Block('bCheckList',Msg(Messages::BUNDLE,Messages::POINTS_DISCUSSED));
$blCk->renderAsPdf()->render($pdf);
$index = 0;
// Build CheckList Form
foreach($monitoring->surveys() as $survey) {
	/* @var $survey Nawa\MedicalStudy\MedicalCenter\Monitoring\Survey */
	$surveyTemplate = $survey->template();
	$blSurvey = new Block('survey_'.$survey->surveyid,$surveyTemplate->label());
	$initialize = null;
	// Retrieve all question answser to initialize the survey
	foreach($survey->questions() as $question) {
		$questions[$question->questionid] = $question;
		$initialize[$question->questionid] = array('default' => $question->answers,'readonly' => true);
	}
	$surveyid = $survey->surveyid;
	$surveyTemplate->renderAsPDF($pdf,$initialize,$index);
	/*
	$blSurvey->add(new \Fox\Html\Html($survey->surveyid,$survey->render($initialize)));
	$blSurvey->collapsed($readonly);
	
	$screen->add($blSurvey);
	*/
}
// Medical Study Header Block
/* @var Fox\Html\Block */
/*
$bHeader = Xview::instance()->component('medicalstudies', 'header');
$bHeader->initialize($msData);
$bHeader->renderAsPdf()->render($pdf);
$pdf->Ln();
// Medical Center Information
$bMCHeader = $xview->component('medicalcenters','head');
$bMCHeader->item('reference')->hidden();
$bMCHeader->add(new Input(array('mc_reference','reference_extern')));
$bMCHeader->initialize($mcData);
$bMCHeader->renderAsPdf()->render($pdf);
// Address Data
$blMcAddress = $xview->component('customers', 'address');
$blMcAddress->label(Msg(Nawa\Messages::BUNDLE,Nawa\Messages::CENTER_ADDRESS));
$blMcAddress->initialize($mcData);
$blMcAddress->renderAsPdf()->render($pdf);
$pdf->Ln();
// Monitoring Data
$blMonitoringHeader = $xview->component('medicalstudies','monitoringHeader',$keys);
$blMonitoringHeader->label(Msg(Nawa\Messages::BUNDLE,Nawa\Messages::MONITORING_INFO));
$blMonitoringHeader->remove('status');
$blMonitoringHeader->initialize($monitoringData);
$blMonitoringHeader->renderAsPdf()->render($pdf);
$pdf->Ln();
//$pdf->Cell(40,10,'Hello World !');
 * */
 
$pdf->Output();